package com.ruoyi.crm.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.domain.Clue;
import com.ruoyi.crm.mapper.BusinessMapper;
import com.ruoyi.crm.mapper.ClueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.AssignRecordMapper;
import com.ruoyi.crm.domain.AssignRecord;
import com.ruoyi.crm.service.IAssignRecordService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 线索分配记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-30
 */
@Service
public class AssignRecordServiceImpl implements IAssignRecordService 
{
    @Autowired
    private AssignRecordMapper assignRecordMapper;
    @Resource
    private ClueMapper clueMapper;
    @Resource
    private BusinessMapper businessMapper;


    /**
     * 查询线索分配记录
     * 
     * @param id 线索分配记录主键
     * @return 线索分配记录
     */
    @Override
    public AssignRecord selectAssignRecordById(Long id)
    {
        return assignRecordMapper.selectAssignRecordById(id);
    }

    /**
     * 查询线索分配记录列表
     * 
     * @param assignRecord 线索分配记录
     * @return 线索分配记录
     */
    @Override
    public List<AssignRecord> selectAssignRecordList(AssignRecord assignRecord)
    {
        return assignRecordMapper.selectAssignRecordList(assignRecord);
    }

    /**
     * 新增线索分配记录
     * 1.更新线索或商机的状态为已分配
     * 2.更新分配记录对应id的latest值为0
     * 3.插入分配记录，latest值为1
     * @param assignRecord 线索分配记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAssignRecord(AssignRecord assignRecord)
    {
        //1.更新线索或商机的状态为已分配
        AssignRecord.Type type = AssignRecord.Type.valueOf(assignRecord.getType());
            if(type == AssignRecord.Type.CLUE){
                 Clue clue = clueMapper.selectClueById(assignRecord.getAssignId());
                 clue.setStatus(Clue.Status.ASSIGNED.getValue());
                 clue.setUpdateBy(SecurityUtils.getUsername());
                 clue.setUpdateTime(new Date());
                 clueMapper.updateClue(clue);
            }else if(type == AssignRecord.Type.BUSINESS){

            }else {
                throw new IllegalArgumentException("枚举类型不存在");
            }
            //2.更新分配记录表
        assignRecordMapper.updateLatest(assignRecord.getAssignId(),type.getValue(),"0");
            //3.插入分配记录,latest值为1
        assignRecord.setLatest("1");
        assignRecord.setType(type.getValue());
        assignRecord.setCreateTime(DateUtils.getNowDate());
            return assignRecordMapper.insertAssignRecord(assignRecord);
    }

    /**
     * 修改线索分配记录
     * 
     * @param assignRecord 线索分配记录
     * @return 结果
     */
    @Override
    public int updateAssignRecord(AssignRecord assignRecord)
    {
        return assignRecordMapper.updateAssignRecord(assignRecord);
    }

    /**
     * 批量删除线索分配记录
     * 
     * @param ids 需要删除的线索分配记录主键
     * @return 结果
     */
    @Override
    public int deleteAssignRecordByIds(Long[] ids)
    {
        return assignRecordMapper.deleteAssignRecordByIds(ids);
    }

    /**
     * 删除线索分配记录信息
     * 
     * @param id 线索分配记录主键
     * @return 结果
     */
    @Override
    public int deleteAssignRecordById(Long id)
    {
        return assignRecordMapper.deleteAssignRecordById(id);
    }

    @Override
    public int insertAssignRecords(List<AssignRecord> assignRecords) {
       int count= 0;
        for (AssignRecord assignRecord : assignRecords) {
            count += this.insertAssignRecord(assignRecord);
        }
        return count;
    }


}
